class MotorcadesController < ApplicationController
  # GET /motorcades
  # GET /motorcades.json
  def index
    @motorcades = Motorcade.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @motorcades }
    end
  end

  # GET /motorcades/1
  # GET /motorcades/1.json
  def show
    @motorcade = Motorcade.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @motorcade }
    end
  end

  # GET /motorcades/new
  # GET /motorcades/new.json
  def new
    @motorcade = Motorcade.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @motorcade }
    end
  end

  # GET /motorcades/1/edit
  def edit
    @motorcade = Motorcade.find(params[:id])
  end

  # POST /motorcades
  # POST /motorcades.json
  def create
    @motorcade = Motorcade.new(params[:motorcade])

    respond_to do |format|
      if @motorcade.save
        format.html { redirect_to @motorcade, notice: 'Motorcade was successfully created.' }
        format.json { render json: @motorcade, status: :created, location: @motorcade }
      else
        format.html { render action: "new" }
        format.json { render json: @motorcade.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /motorcades/1
  # PUT /motorcades/1.json
  def update
    @motorcade = Motorcade.find(params[:id])

    respond_to do |format|
      if @motorcade.update_attributes(params[:motorcade])
        format.html { redirect_to @motorcade, notice: 'Motorcade was successfully updated.' }
        format.json { head :ok }
      else
        format.html { render action: "edit" }
        format.json { render json: @motorcade.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /motorcades/1
  # DELETE /motorcades/1.json
  def destroy
    @motorcade = Motorcade.find(params[:id])
    @motorcade.destroy

    respond_to do |format|
      format.html { redirect_to motorcades_url }
      format.json { head :ok }
    end
  end

    # 接收数据报
  # POST /motor_cades/1/receive_datagram
  def receive_datagram
    motorcade_data = ActiveSupport::JSON.decode params[:data]
    motor_cade = MotorCade.find_by_code(params[:id])
    motor_cade.datagram_save(motorcade_data, params[:id])
    motor_cade.read_datagram(motorcade_data)
    render :text => '200'
  end

  skip_before_filter :require_login, :only => :receive_datagram
end
